<?php

class MemberDAL {
	
	private $m_dbConnection;
	
	public function __construct ($a_dbConnection){
		$this->m_dbConnection = $a_dbConnection;	
	}
	
	public function DeleteMember ($memberId){
		$sql = "DELETE FROM `member` WHERE `MemberId` = ? ";
		$stmt = $this->m_dbConnection->Prepare($sql);
		$stmt -> bind_param("i", $memberId);
		
		if ($stmt->execute()){
			return true;
		}else {
			return false;
		}
	}
	
	public function GetMemberId ($ssn){
		$sql = "SELECT `MemberId`  FROM `member` WHERE `Ssn` = ? ";
		$stmt = $this->m_dbConnection->Prepare($sql);
		$stmt -> bind_param("s", $ssn);
		$stmt->execute();
		
		
		$stmt->bind_result($memberId);
		$stmt->fetch();
		
       	if ($memberId != false){
		   return $memberId;
						
		}else {
			return false;
		}
	}

	public function AddMember ($a_firstName, $a_ssn){
	$sql = "INSERT INTO  `workshop2`.`member` (`MemberId` ,`Name` ,`Ssn`)
			VALUES (NULL ,  ?,  ?)";
	$stmt = $this->m_dbConnection->Prepare($sql);
	$stmt -> bind_param("ss", $a_firstName, $a_ssn);

	if ($stmt->execute()){
		return true;
	}else {
		return false;
	}
	}
		
	public function GetMembers () {
		$sql= "SELECT `Name`, `Ssn`, `MemberId`  FROM `member`";
		$stmt = $this->m_dbConnection->Prepare($sql);
		$stmt->bind_result($name, $ssn, $memberId);
		
		$stmt->execute();
		
		$i = 0;
		$member = new Member;
		$memberArray = array();
		
		while($stmt->fetch()){
			$memberArray [$i] = $member->Create($memberId, $name, $ssn); 
			$i++;
		}
		
		return $memberArray;
	}
}